﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;
using System.Collections;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Utility;
using System.IO;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class BannerAdController : AdminController
    {
        //
        // GET: /BannerAd/

        public BannerAdController()
        {
        }

        public ActionResult Index()
        {
            if (Request.QueryString.Get("page") == null)
                return ListAllBannerAd(1);
            else
                return ListAllBannerAd(int.Parse(Request.QueryString.Get("page")));
        }
        public ActionResult ListAllBannerAd(int? page)
        {
            BannerAdHelper hMem = new BannerAdHelper();
            return ListBannerAd(page, new ListBannerAdDelegate(hMem.getAllBannerAds));
        }

        private delegate BannerAdList ListBannerAdDelegate(int page, int rowPerPage);

        private ActionResult ListBannerAd(int? page, ListBannerAdDelegate delListMem)
        {
            int currentPage = 1;
            if (page != null)
                currentPage = (int)page;
            BannerAdHelper hMem = new BannerAdHelper();
            BannerAdList lstNewMembers = delListMem(currentPage, Config.ITEMS_PER_PAGE);

            lstNewMembers.CurrentPage = currentPage;
            return View("~/Views/Admin/ListBannerAd.aspx", lstNewMembers);
        }

        public ActionResult CreateBannerAd()
        {
            if (Request.QueryString.Get("bannerId") == null)
            {
            }
            else
            {
                BannerAdHelper bannerHelper = new BannerAdHelper();
                BannerAd en = bannerHelper.getBanner(int.Parse(Request.QueryString.Get("bannerId")));
                ViewData["BannerId"] = en.BannerId;
                ViewData["BannerName"] = en.BannerText;
                ViewData["BannerClick"] = en.BannerText;
                ViewData["BannerStartDay"] = en.BannerStartDate;
                ViewData["BannerEndDay"] = en.BannerEndDate;
                ViewData["BannerPos"] = en.BannerPosition;
                ViewData["BannerCus"] = en.CusId;
                ViewData["bannerImg"] = en.BannerImg;
                ViewData["BannerURL"] = en.BannerLink;
                DateTime start = (DateTime)en.BannerStartDate;
                ViewData["BannerStartDate"] = (start.Day.ToString().Length == 1 ? "0" + start.Day + "" : start.Day + "") + "/" + (start.Month.ToString().Length == 1 ? "0" + start.Month + "" : start.Month + "") + "/" + start.Year;
                DateTime end = (DateTime)en.BannerEndDate;
                ViewData["BannerEndDate"] = (end.Day.ToString().Length == 1 ? "0" + end.Day + "" : end.Day + "") + "/" + (end.Month.ToString().Length == 1 ? "0" + end.Month + "" : end.Month + "") + "/" + end.Year;

            }
            return View("~/Views/Admin/CreateBannerAd.aspx");
        }

        public ActionResult AjaxSaveBannerAd(string id, string link, string text, string img, string pos, string memid, string status, string pageid, string startday, string endday, string type, string cusid)
        {
            if (id.Equals("0"))
            {
                BannerAd ban = new BannerAd();
                ban.BannerEndDate = DateTime.ParseExact(endday, "dd/MM/yyyy", null);
                ban.BannerImg = img;
                ban.BannerLink = link;
                ban.BannerPageId = null;
                ban.BannerPosition = int.Parse(pos);
                ban.BannerStartDate = DateTime.ParseExact(startday, "dd/MM/yyyy", null);
                ban.BannerStatus = true;
                ban.BannerText = text;
                ban.BannerType = type;
                ban.CusId = int.Parse(cusid);
                new BannerAdHelper().saveBannerAd(ban);
            }
            else
            {
                BannerAd ban = new BannerAd();
                ban.BannerId = int.Parse(id);
                ban.BannerEndDate = DateTime.ParseExact(endday, "dd/MM/yyyy", null);
                ban.BannerImg = img;
                ban.BannerLink = link;
                ban.BannerPageId = int.Parse(pageid);
                ban.BannerPosition = int.Parse(pos);
                ban.BannerStartDate = DateTime.ParseExact(startday, "dd/MM/yyyy", null);
                ban.BannerStatus = true;
                ban.BannerText = text;
                ban.BannerType = type;
                ban.CusId = int.Parse(cusid);
                new BannerAdHelper().saveBannerAd(ban);
            }
            return ListAllBannerAd(1);
        }

        public ActionResult SaveBannerAd(FormCollection form)
        {
            if (form.Get("txtBannerId").Equals(""))
            {
                BannerAd en = new BannerAd();
                en.BannerEndDate = DateTime.ParseExact(form.Get("txtBannerNgayKetThuc"), "dd/MM/yyyy", null);
                if (form.Get("txtBannerLink").Equals(""))
                {
                    if (form.Get("txtBannerImg").StartsWith("http://"))
                        en.BannerImg = form.Get("txtBannerImg");
                    else
                    {
                        foreach (string file in Request.Files)
                        {
                            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                            ImageHelper imgHelper = new ImageHelper();
                            string fileName = hpf.FileName;
                            string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/"), hpf.FileName);
                            en.BannerImg = "~/Content/Images/Upload/" + fileName;
                            imgHelper.uploadImage(path, hpf, 200, 200);

                        }
                    }
                }
                else
                {
                    en.BannerImg = form.Get("txtBannerLink");
                }
                en.BannerLink = form.Get("txtBannerClick");
                en.BannerPosition = int.Parse(form.Get("drlBannerViTri"));
                en.BannerStartDate = DateTime.ParseExact(form.Get("txtBannerNgayBatDau"), "dd/MM/yyyy", null);
                en.BannerStatus = true;
                en.BannerText = form.Get("txtBannerName");
                if (en.BannerImg.EndsWith(".swf") || en.BannerImg.EndsWith("flv"))
                    en.BannerType = "flash";
                else en.BannerType = "image";
                en.CusId = int.Parse(form.Get("drlBannerCus"));
                en.BannerLink = form.Get("txtBannerURL");
                BannerAdHelper cusHelper = new BannerAdHelper();
                cusHelper.saveBannerAd(en);
            }
            else
            {
                BannerAdHelper hMem = new BannerAdHelper();
                BannerAd en = hMem.getBanner(int.Parse(form.Get("txtBannerId")));
                en.BannerEndDate = DateTime.ParseExact(form.Get("txtBannerNgayKetThuc"), "dd/MM/yyyy", null);
                if (form.Get("txtBannerLink").Equals(""))
                {
                    if (form.Get("txtBannerImg").StartsWith("http://"))
                        en.BannerImg = form.Get("txtBannerImg");
                    else
                    {
                        foreach (string file in Request.Files)
                        {
                            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                            ImageHelper imgHelper = new ImageHelper();
                            string fileName = hpf.FileName;
                            string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/"), hpf.FileName);
                            en.BannerImg = fileName;
                            imgHelper.uploadImage(path, hpf, 200, 200);

                        }
                    }
                }
                else
                {
                    en.BannerImg = form.Get("txtBannerLink");
                }
                en.BannerLink = form.Get("txtBannerClick");
                en.BannerPosition = int.Parse(form.Get("drlBannerViTri"));
                en.BannerStartDate = DateTime.ParseExact(form.Get("txtBannerNgayBatDau"), "dd/MM/yyyy", null);
                en.BannerStatus = true;
                en.BannerText = form.Get("txtBannerName");
                if (en.BannerImg.EndsWith(".swf") || en.BannerImg.EndsWith("flv"))
                    en.BannerType = "flash";
                else en.BannerType = "image";
                en.CusId = int.Parse(form.Get("drlBannerCus"));
                en.BannerLink = form.Get("txtBannerURL");
                hMem.saveBannerAd(en);
            }
            return ListAllBannerAd(1);
        }

        [HttpPost]
        public ActionResult DeleteBannerAd(string cusId)
        {
            BannerAdHelper hMem = new BannerAdHelper();
            hMem.DeleteBannerAd(cusId);
            return ListAllBannerAd(1);
        }

        [HttpPost]
        public void viewBannerAd(string bannerId)
        {
            BannerAdHelper hMem = new BannerAdHelper();
            BannerAd en = hMem.getBanner(int.Parse(bannerId));
            ViewData["BannerId"] = en.BannerId;
            ViewData["BannerName"] = en.BannerText;
            ViewData["BannerClick"] = en.BannerText;
            ViewData["BannerStartDay"] = en.BannerStartDate;
            ViewData["BannerEndDay"] = en.BannerEndDate;
            ViewData["BannerPos"] = en.BannerPosition;
            ViewData["BannerCus"] = en.CusId;
            ViewData["bannerImg"] = en.BannerImg;
            ViewData["BannerURL"] = en.BannerLink;
        }
        public ActionResult ChangeStatusCustomer(string cusId)
        {
            try
            {
                CustomerHelper cusHelper = new CustomerHelper();
                Customer cus = cusHelper.getCustomer(int.Parse(cusId));
                if (cus.CusStatus == 1)
                    cus.CusStatus = 0;
                else
                    cus.CusStatus = 1;
                cusHelper.Save();
            }
            catch (Exception)
            {
            }
            return Index();
        }
    }
}
